package com.zk.shop.servlet.Cart;

import com.zk.shop.entity.CartItem;
import com.zk.shop.entity.Goods;
import com.zk.shop.entity.User;
import com.zk.shop.service.CartService;
import com.zk.shop.service.GoodsService;
import com.zk.shop.service.Impl.CartServiceImpl;
import com.zk.shop.service.Impl.GoodsServicesImpl;
import com.zk.shop.util.CartCookieUtil;
import com.zk.shop.util.JSONServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 购物车添加商品
 */
@WebServlet("/addCartServlet")
public class addCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = new CartServiceImpl();
        GoodsService goodsService = new GoodsServicesImpl();

        int goodsId = Integer.parseInt(request.getParameter("goodsId"));
        Goods goods = goodsService.getThisGoodsById(goodsId);
        int buyNum = Integer.parseInt(request.getParameter("buyNum"));
        User user = (User) request.getSession().getAttribute("user");
        CartItem  cartItem = new CartItem(user,goods,buyNum);
        //标志购买数量是否小于库存数量
        boolean flag = false;
        if(goods.getGoodsNum() >= buyNum){
            flag = true;
        }

        if(flag){
            List<CartItem> cartItemList = CartCookieUtil.getCart(request);
            Cookie cartCookie;
            //如果用户未登录 用cookie存储商品信息
            if(user == null){
                //如果购物列表为空
                if(cartItemList.size() <= 0){
                    cartItemList.add(cartItem);
                    //如果没有cart cookie
                    if(CartCookieUtil.getCookie(request) == null){
                        cartCookie = new Cookie("cart", URLEncoder.encode(CartCookieUtil.makeCookieValue(cartItemList),"UTF-8"));
                        cartCookie.setPath("/");
                        //设置cookie有效期为3天
                        cartCookie.setMaxAge(60 * 60 * 24 * 3);
                        cartCookie.setValue(URLEncoder.encode(CartCookieUtil.makeCookieValue(cartItemList),"UTF-8"));
                        response.addCookie(cartCookie);
                    } else{ //如果有cart cookie
                        cartCookie = CartCookieUtil.getCookie(request);
                        cartCookie.setPath("/");
                        cartCookie.setMaxAge(60 * 60 * 24 * 3);
                        cartCookie.setValue(URLEncoder.encode(CartCookieUtil.makeCookieValue(cartItemList),"UTF-8"));
                        response.addCookie(cartCookie);
                    }
                } else{ //如果购物列表不为空
                    boolean iFlag = false;
                    //遍历如果要添加的商品存在，则数量相加
                    for(CartItem item : cartItemList){
                        if(item.getGoods().getGoodsId() == goodsId){
                            item.setBuyNum(item.getBuyNum() + buyNum);
                            iFlag = true;
                            break;
                        }
                    }
                    //如果不存在该商品则添加该商品
                    if(!iFlag){
                        cartItemList.add(cartItem);
                    }
                    //设置cookie
                    cartCookie = CartCookieUtil.getCookie(request);
                    cartCookie.setPath("/");
                    cartCookie.setMaxAge(60 * 60 * 24 * 3);
                    cartCookie.setValue(URLEncoder.encode(CartCookieUtil.makeCookieValue(cartItemList),"UTF-8"));
                    response.addCookie(cartCookie);
                }
            }else{  //如果用户已登录 持久化
                int result = cartService.selGoods(cartItem);
                //如果存在该商品
                if(result > 0){
                    cartService.updateBuyNum(cartItem);
                }else{  //如果不存在该商品
                    cartService.addCart(cartItem);
                }
            }
        }
        JSONServlet.getJson(request,response,flag);

    }
}
